package cn.pn.project.mapper.base;

import cn.pn.project.module.base.AddressModule;
import cn.pn.project.pojo.base.Address;
import org.apache.ibatis.annotations.*;

import java.sql.SQLException;
import java.util.List;

/**
 * 用户
 */
@Mapper
public interface AddressDao {
    /**
     * 添加地址
     * @param address
     * @return
     * @throws SQLException
     */
    @Insert("INSERT INTO `pn_base`.`address` (  `addressId`,  `userId`,  `receiveName`,  `receiveAddress`,  `telephone`,  `more1`,  `more2`,  `more3`,  `isDefaultAddress`,  `provinceId`,  `cityId`,  `countyId`)VALUES  (   #{address.addressId},   #{address.userId},   #{address.receiveName},  #{address.receiveAddress},   #{address.telephone},   #{address.more1},   #{address.more2},   #{address.more3},   #{address.isDefaultAddress},   #{address.provinceId},   #{address.cityId},   #{address.countyId});")
    Integer insertAddress(@Param("address") Address address) throws SQLException;

    /**
     * 删除地址
     * @param addressId
     * @return
     * @throws SQLException
     */
    @Delete("UPDATE `pn_base`.`address` SET  `isExist`=2 WHERE `addressId`=#{addressId}")
    Integer deleteAddress(@Param("addressId") Integer addressId) throws SQLException;

    /**
     * 更新地址
     * @param address id 数据库中必须存在此id
     * @return
     * @throws SQLException
     */
    @Update("UPDATE  `pn_base`.`address`SET  `userId` =#{address.userId},  `receiveName` =#{address.receiveName},  `receiveAddress` =#{address.receiveAddress},  `telephone` =#{address.telephone},  `more1` =#{address.more1},  `more2` =#{address.more2},  `more3` =#{address.more3},  `isDefaultAddress` =#{address.isDefaultAddress},  `provinceId` =#{address.provinceId},  `cityId` =#{address.cityId},  `countyId` =#{address.countyId} where `addressId` =#{address.addressId};")
    Integer updateAddress(@Param("address") Address address) throws SQLException;

    /**
     * 查询地址
     * @param  address  userId 可以为空 查询所有地址 或者查询某用户的所有地址  isDefaultAddress 可以为空 查询查询所有地址 或者查询所有默认地址 配合userId 可查询某用户下的默认地址
     * @return
     * @throws SQLException
     */
    @Select("<script>" +
            "SELECT address.`addressId`, address.`userId`,address.`receiveName`,address.`receiveAddress`,address.`telephone`, address.`isDefaultAddress`,address.`provinceId`,address.`cityId`,address.`countyId`,c1.`cityName` AS provinceName,c2.`cityName` AS cityName,c3.`cityName`AS countyName,address.`isExist` FROM `pn_base`.`address` address\n" +
            "INNER JOIN city c1 ON c1.`cityId`=address.`provinceId`\n" +
            "INNER JOIN city c2 ON c2.`cityId`=address.`cityId`\n" +
            "INNER JOIN city c3 ON c3.`cityId`=address.`countyId`\n" +
            "          <where>\n" +
            "              <if test='address.userId!=null and  address.userId!=0'>\n" +
            "                and userId=#{address.userId} and `isExist`=1\n" +
            "              </if>\n" +
            "              <if test='address.isDefaultAddress!=null'>\n" +
            "                  and isDefaultAddress=#{address.isDefaultAddress}\n" +
            "              </if>\n" +
            "              " +
            "          </where>" +
            "</script>")
    List<AddressModule> selectAddress(@Param("address")  Address address) throws SQLException;

    /**
     * 根据地址id 查询地址
     * @param addressId
     * @return
     * @throws SQLException
     */
    @Select("select * from  `pn_base`.`address` where `addressId`=#{addressId} and `isExist`=1 ")
    AddressModule findAddressByAddressId(@Param("addressId")Integer addressId)throws SQLException;
}
